package io.yuncheng.common.utils;

import io.yuncheng.modules.sys.entity.SubAppAlgorithmLogCollectArithTimeEntity;
import io.yuncheng.modules.sys.entity.SubAppAlgorithmLogCollectPathEntity;
import io.yuncheng.modules.sys.entity.SubAppAlgorithmLogEntity;
import org.apache.commons.lang.StringUtils;

import java.math.BigDecimal;
import java.util.LinkedHashMap;
import java.util.Map;

/**
 * 数字工具类
 */
public class NumberUtil {


    /**
     * 保存文件到
     *
     * @param long1
     * @param long2
     * @return
     */
    public static Long longAdd(Long long1, Long long2) {
        if (long1 == null) {
            long1 = 0L;
        }
        if (long2 == null) {
            long2 = 0L;
        }
        return long1 + long2;
    }

    /**
     * string 转integer
     * @param string
     * @return
     */
    public static Integer stringToInteger(String string){
        Integer i = 0;
        if(StringUtils.isNotBlank(string)){
            try {
                i = Integer.valueOf(string);
            }catch (Exception e){
                e.printStackTrace();
            }
        }
       return i;
    }

    public static Integer integerAdd(Integer long1, Integer long2) {
        if (long1 == null) {
            long1 = 0;
        }
        if (long2 == null) {
            long2 = 0;
        }
        return long1 + long2;
    }

    public static String stringAdd(String str1, String str2) {
        int i = 0;
        int j = 0;
        if(str1 != null){
            i = Integer.valueOf(str1);
        }
        if(str2 != null){
            j = Integer.valueOf(j);
        }
        return String.valueOf(i + j) ;
    }

    /**
     * 求取比例
     *
     * @param count
     * @param total
     * @return
     */
    public static BigDecimal getPercent(long count, long total) {
        if (total == 0) {
            return BigDecimal.ZERO;
        }
        BigDecimal currentCount = new BigDecimal(count);
        BigDecimal totalCount = new BigDecimal(total);
        BigDecimal divide = currentCount.divide(totalCount, 4, BigDecimal.ROUND_HALF_UP);
        return divide;
    }

    /**
     * long ÷ count
     *
     * @param time
     * @param count
     * @return
     */
    public static BigDecimal getPercentTimeCount(Long time, Integer count) {
        if (time == null || count == null || time == 0L || count == 0) {
            return BigDecimal.ZERO;
        }
        BigDecimal timeBig = new BigDecimal(time);
        BigDecimal bigCount = new BigDecimal(count);
        BigDecimal divide = timeBig.divide(bigCount, 0, BigDecimal.ROUND_HALF_UP);
        return divide;
    }

    public static void addEntityParentChild(SubAppAlgorithmLogCollectArithTimeEntity collect, SubAppAlgorithmLogCollectArithTimeEntity child) {
        collect.setAlgorithmOne(integerAdd(collect.getAlgorithmOne(), child.getAlgorithmOne()));

        collect.setAlgorithmTimeOne(longAdd(collect.getAlgorithmTimeOne(), child.getAlgorithmTimeOne()));
        collect.setAlgorithmTimeTwo(longAdd(collect.getAlgorithmTimeTwo(), child.getAlgorithmTimeTwo()));

        collect.setAlgorithmTwo(integerAdd(collect.getAlgorithmTwo(), child.getAlgorithmTwo()));
        collect.setAlgorithmThree(integerAdd(collect.getAlgorithmThree(), child.getAlgorithmThree()));
        collect.setAlgorithmFour(integerAdd(collect.getAlgorithmFour(), child.getAlgorithmFour()));
        collect.setAlgorithmFive(integerAdd(collect.getAlgorithmFive(), child.getAlgorithmFive()));


        collect.setAlgorithmTimeThree(longAdd(collect.getAlgorithmTimeThree(), child.getAlgorithmTimeThree()));
        collect.setAlgorithmTimeFour(longAdd(collect.getAlgorithmTimeFour(), child.getAlgorithmTimeFour()));
        collect.setAlgorithmTimeFive(longAdd(collect.getAlgorithmTimeFive(), child.getAlgorithmTimeFive()));
    }

    public static void addEntity(SubAppAlgorithmLogCollectArithTimeEntity collect, SubAppAlgorithmLogEntity logEntity) {
        collect.setAlgorithmOne(integerAdd(collect.getAlgorithmOne(), logEntity.getAlgorithmOne()));
        collect.setAlgorithmTwo(integerAdd(collect.getAlgorithmTwo(), logEntity.getAlgorithmTwo()));
        collect.setAlgorithmThree(integerAdd(collect.getAlgorithmThree(), logEntity.getAlgorithmThree()));
        collect.setAlgorithmFour(integerAdd(collect.getAlgorithmFour(), logEntity.getAlgorithmFour()));
        collect.setAlgorithmFive(integerAdd(collect.getAlgorithmFive(), logEntity.getAlgorithmFive()));

        collect.setAlgorithmTimeOne(longAdd(collect.getAlgorithmTimeOne(), logEntity.getAlgorithmTimeOne()));
        collect.setAlgorithmTimeTwo(longAdd(collect.getAlgorithmTimeTwo(), logEntity.getAlgorithmTimeTwo()));
        collect.setAlgorithmTimeThree(longAdd(collect.getAlgorithmTimeThree(), logEntity.getAlgorithmTimeThree()));
        collect.setAlgorithmTimeFour(longAdd(collect.getAlgorithmTimeFour(), logEntity.getAlgorithmTimeFour()));
        collect.setAlgorithmTimeFive(longAdd(collect.getAlgorithmTimeFive(), logEntity.getAlgorithmTimeFive()));
        collect.setExeTime(longAdd(collect.getExeTime(), logEntity.getExeTime()));
    }

    public static void addEntity(SubAppAlgorithmLogCollectPathEntity collect, SubAppAlgorithmLogEntity logEntity) {
        collect.setAlgorithmTimeOne(logEntity.getExeTime());
        collect.setExeTime(longAdd(collect.getExeTime(), logEntity.getExeTime()));
    }

    /**
     * 返回算法对应的次数
     *
     * @param entity
     * @return
     */
    public static Map<Integer, Integer> buildMapByTimeEntity(SubAppAlgorithmLogCollectArithTimeEntity entity) {
        Map<Integer, Integer> map = new LinkedHashMap<>(16);
        map.put(1, entity.getAlgorithmOne());
        map.put(2, entity.getAlgorithmTwo());
        map.put(3, entity.getAlgorithmThree());
        map.put(4, entity.getAlgorithmFour());
        //map.put(5,entity.getAlgorithmFive());
        return map;
    }

    /**
     * 返回算法对应的次数
     *
     * @param entity
     * @return
     */
    public static Map<Integer, BigDecimal> buildAvgTime(SubAppAlgorithmLogCollectArithTimeEntity entity) {
        Map<Integer, BigDecimal> map = new LinkedHashMap<>(16);
        map.put(1, getPercentTimeCount(entity.getAlgorithmTimeOne(), entity.getAlgorithmOne()));
        map.put(2, getPercentTimeCount(entity.getAlgorithmTimeTwo(), entity.getAlgorithmTwo()));
        map.put(3, getPercentTimeCount(entity.getAlgorithmTimeThree(), entity.getAlgorithmThree()));
        map.put(4, getPercentTimeCount(entity.getAlgorithmTimeFour(), entity.getAlgorithmFour()));
        // map.put(5,getPercentTimeCount(entity.getAlgorithmTimeFive(),entity.getAlgorithmFive()));
        return map;
    }


}
